package com.kitty.game.system;


import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.role.model.Role;
import com.kitty.game.system.message.ReqSetSetting;
import com.kitty.game.system.message.RespSettingInfo;
import com.kitty.game.system.message.vo.ReqShock;
import com.kitty.game.system.message.vo.RespShock;
import com.kitty.game.system.message.vo.SettingInfo;
import com.kitty.game.team.message.RespMsg;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.SwitchChangeEvent;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Map;

@Controller
public class SetController {

    @RequestMapping
    public void set(IoSession session, ReqSetSetting reqSetSetting) {
        Role role = SessionUtils.getRoleBySession(session);
        Map<String, Integer> setMap = role.getExtendBox().getSetMap();
        setMap.put(reqSetSetting.getKey(), 0 + reqSetSetting.getValue());
//        if (reqSetSetting.getKey().equals("sight_scope")){
//            setMap.put(reqSetSetting.getKey(), 2);
//        }
        role.save();
        RespSettingInfo respSettingInfo = new RespSettingInfo();
        ArrayList<SettingInfo> settingList = new ArrayList<>();
        {
            SettingInfo settingInfo = new SettingInfo();
            settingInfo.setSettingVaule(reqSetSetting.getValue());
            settingInfo.setSettingName(reqSetSetting.getKey());
//            if (settingInfo.getSettingName().equals("sight_scope")){
//                settingInfo.setSettingVaule((short) 2);
//            }
            settingList.add(settingInfo);
        }
        respSettingInfo.setList(settingList);
        MessagePusher.pushMessage(session, respSettingInfo);
        EventDispatcher.getInstance().fireEvent(new SwitchChangeEvent(EventType.SWITCH_CHANGE, role, reqSetSetting.getKey(), reqSetSetting.getValue()==1));
    }

    /**
     * 申请振动其他玩家
     */
    @RequestMapping
    public void shock(IoSession session, ReqShock reqShock) {
        Role role = SessionUtils.getRoleBySession(session);
        Role target = SpringUtils.getPlayerService().getPlayerBy(Long.parseLong(reqShock.getGid()));

        MessagePusher.pushMessage(session, new RespMsg("已向#Y" + target.getName() + "#n发送震动提醒。"));

        MessagePusher.pushMessage(target, new RespShock());
        MessagePusher.pushMessage(target, new RespNotifyMiscEx("#Y" + role.getName() + "#n在队伍中向你发送了一次震动提醒。"));
    }
}